package helper

import "github.com/astaxie/beego/orm"

type Api struct {
	Code       int         `json:"code"`
	Data       interface{} `json:"data"`
	Message    string      `json:"message"`
	Pagination interface{} `json:"pagination,omitempty"`
}

type Pagination struct {
	CurrentPage int `json:"current_page"`
	Limit       int `json:"limit"`
	TotalPage   int `json:"total_page"`
	Total       int `json:"total"`
}

func NewApi() *Api {
	return new(Api)
}

func (a *Api) SetPagination(CurrentPage int, Limit int, TotalPage int, Total int) *Api {
	var pagination = Pagination{}
	pagination.CurrentPage = CurrentPage
	pagination.Limit = Limit
	pagination.Total = Total
	pagination.TotalPage = TotalPage
	a.Pagination = pagination
	return a
}

func (a *Api) Success(data interface{}, message ...string) *Api {
	switch data.(type) {
	case []orm.Params:
		if len(data.([]orm.Params)) == 0 {
			a.Data = []int{}
		} else {
			a.Data = data
		}
		break
	default:
		a.Data = data
	}

	a.Code = 0
	if len(message) > 0 {
		a.Message = message[0]
	} else {
		a.Message = ""
	}
	return a
}

func (a *Api) Error(Message ...string) *Api {
	a.Data = ""
	a.Code = 1
	if len(Message) > 0 {
		a.Message = Message[0]
	} else {
		a.Message = ""
	}
	return a
}
